'use strict';

// const Controller = require('egg').Controller;

const baseController = require('../../utils/base')
class AccessController extends baseController {
    async index() {

        let result = await this.ctx.model.Access.findAll({
            where: {
                moduleId: 0
            },
            include: { model: this.ctx.model.Access }
        });
        await this.ctx.render('admin/access/index', {
            list: result
        })
    }
    async add() {
        var sql = "select * from access where module_id = 0"
        let accessList = await this.app.mysql.query(sql)
        await this.ctx.render('admin/access/add', { accessList })
    }
    async doAdd() {
        // {"_csrf":"I3hCSPKq-SsVa5IL1M4xCoUU_tYcdaSoGL_Q","moduleName":"","type":"1","actionName":"","url":"","moduleId":"0","sort":"100","description":""}
        let { moduleName, type, actionName, url, moduleId, sort, description } = this.ctx.request.body
        var sql = 'insert into access ( module_name, action_name, type, url, status, module_id, sort, description, add_time) VALUES (?,?,?,?,?,?,?,?,?)'
        let result = await this.app.mysql.query(sql, [moduleName, actionName, type, url, 1, moduleId, sort, description, await this.service.admin.utlis.getDate()])
        if (result.protocol41) {
            await this.success('添加成功', `/${this.config.pathname}/access`)
        } else {
            await this.error('添加失败', `/${this.config.pathname}/access/add`)
        }
    }
    async edit() {
        let { id } = this.ctx.query
        var sql = "select * from access where module_id = 0"
        let accessList = await this.app.mysql.query(sql)

        var sql = "select * from access where id = ?"
        var result =  await this.app.mysql.query(sql,[id])
        console.log(result,"====");

        await this.ctx.render('admin/access/edit', {
            accessList,
            result:result[0]
        })
        // this.ctx.body = this.ctx.query
    }
    async doEdit() {
        let { moduleName, type, actionName, url, moduleId, sort, description ,id} = this.ctx.request.body
        var sql = 'update access set module_name=?,action_name=?,type=?,url=?,module_id=?,sort=?,description=? where id=?'
        let result = await this.app.mysql.query(sql, [moduleName, actionName, type, url, moduleId, sort, description,id])
        if (result.protocol41) {
            await this.success('添加成功', `/${this.config.pathname}/access`)
        } else {
            await this.error('添加失败', `/${this.config.pathname}/access/add`)
        }
    }
    async del() {
        try {
            let { id } = this.ctx.query
            let result = await this.app.mysql.query('delete from access where id = ?', [id])
            if (result.protocol41) {
                await this.success('删除成功', `/${this.config.pathname}/access`)
            } else {
                await this.success('删除成功', `/${this.config.pathname}/access`)
            }
        } catch (error) {
            await this.success('系统异常错误', `/${this.config.pathname}/access`)
        }
    }
}

module.exports = AccessController;
